package app.fugema.shop.main.goods.adapter;

import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.base.common.utils.DensityUtil;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleUniversalAdapter;
import com.base.common.view.adapter.connector.BaseViewHolder;
import app.fugema.shop.R;
import app.fugema.shop.main.goods.model.CateGoodsPageBean;
import app.fugema.shop.main.goods.view.GoodsDetailActivity;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

public class ListProductAdapter extends SingleUniversalAdapter<CateGoodsPageBean.ProductBean>
{

    private int layRes;

    public ListProductAdapter(@LayoutRes int layRes)
    {
        this.layRes = layRes;
        super.init();
    }



    @Override
    public int getLayoutId()
    {
        return this.layRes;
    }


    public void reBuildLayout(@LayoutRes int layRes)
    {
        this.layRes = layRes;
        notifyDataSetChanged();
    }


    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position)
    {
        super.onBindViewHolder(holder, position);
        if (holder.getAdapter() != null)
        {
            ((TextView)holder.itemView.findViewById(R.id.tv_delete_price)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

            int pos = getFirstPosition(holder.getAdapter());
            if (pos != -1) {

                if (holder.itemView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
                    int spc = position - pos;
                    if (spc % 2 == 0) {
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_15);
                    } else {
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_5);
                    }
                    holder.itemView.setLayoutParams(marginLayoutParams);
                }
            }
        }
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, CateGoodsPageBean.ProductBean bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        GoodsDetailActivity.start(view.getContext(), bean.getProductId());
    }

    private int getFirstPosition(BaseRVAdapter adapter)
    {
        return adapter.getFirstPosition(getItemViewType(), 0);
    }
}
